package com.example.project.service;


import com.example.project.dao.UserInterface;
import com.example.project.entity.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserService {
    @Resource
    UserInterface userInterface;

    public ResponseInfo login(User user) {

        User res = userInterface.login(user);
        if (res != null) {
            String token = UUID.randomUUID().toString();
            Map map = new HashMap();
            res.setToken(token);
            map.put("token", res.getToken());
            map.put("user", res);
            userInterface.updateToken(res);

            // 生成token
            //  存入数据库mysql、redis
            // 返回token给前端vue

            return ResponseInfo.success(map);
        } else {
            return ResponseInfo.fail(205, "账号或密码错误");
        }
    }
    public ResponseInfo getusertable(User user, RequestInto params) {
        params.setStart((params.getPage()-1)*params.getSize());
        List<Menu> list = userInterface.queryTable(user,params);
        long total=userInterface.queryTableCount(user,params);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("total", total);

        return  ResponseInfo.success(map);
    }

    public ResponseInfo adduser(@RequestBody User user) {
        userInterface.adduser(user);
        return  ResponseInfo.success(null);
    }

    public ResponseInfo edituser(User user) {
        userInterface.edituser(user);
        return  ResponseInfo.success(null);
    }

    public ResponseInfo deluser(User user) {
        userInterface.deluser(user);
        return  ResponseInfo.success(null);
    }

//    6.1查找管理员
public ResponseInfo getadmin() {
    List<User> list = userInterface.getadmin();
    long adminCount=userInterface.adminCount();
    System.out.println(list);
    Map<String, Object> map = new HashMap<>();
    map.put("rows", list);
    map.put("count", adminCount);
    return ResponseInfo.success(map);
}
}
